package xcmg.syn.util;


import java.security.MessageDigest;
import yb.ecp.fast.infra.infra.log.LogHelper;

public class SecretUtils {

  private static final char[] BASE64_ENCODE_CHARS = new char[]{
      'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
      'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
      'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
      'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
      'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
      'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
      'w', 'x', 'y', 'z', '0', '1', '2', '3',
      '4', '5', '6', '7', '8', '9', '+', '/'
  };


  /**
   * 对字符串md5加密
   *
   * @param str
   * @return
   */
  public static String genMD5(String str) {
    MessageDigest messageDigest = null;
    try {
      messageDigest = MessageDigest.getInstance("MD5");
      messageDigest.reset();

      messageDigest.update(str.getBytes("UTF-8"));
    } catch (Exception e) {
      LogHelper.fatal("MD5算法错误", e);
    }
    byte[] byteArray = messageDigest.digest();

    StringBuffer md5StrBuff = new StringBuffer();

    for (int i = 0; i < byteArray.length; i++) {
      // 原先的方法未做处理导致加密后的结果如果以0开头，会丢失这个0
      if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) {
        md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
      } else {
        md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
      }
    }
    return md5StrBuff.toString();
  }

  /**
   * base64Encode
   *
   * @param data
   * @return
   */
  public static String base64Encode(byte[] data) {
    StringBuffer sb = new StringBuffer();
    int len = data.length;
    int i = 0;
    int b1, b2, b3;
    while (i < len) {
      b1 = data[i++] & 0xff;
      if (i == len) {
        sb.append(BASE64_ENCODE_CHARS[b1 >>> 2]);
        sb.append(BASE64_ENCODE_CHARS[(b1 & 0x3) << 4]);
        sb.append("==");
        break;
      }
      b2 = data[i++] & 0xff;
      if (i == len) {
        sb.append(BASE64_ENCODE_CHARS[b1 >>> 2]);
        sb.append(BASE64_ENCODE_CHARS[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]);
        sb.append(BASE64_ENCODE_CHARS[(b2 & 0x0f) << 2]);
        sb.append("=");
        break;
      }
      b3 = data[i++] & 0xff;
      sb.append(BASE64_ENCODE_CHARS[b1 >>> 2]);
      sb.append(BASE64_ENCODE_CHARS[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]);
      sb.append(BASE64_ENCODE_CHARS[((b2 & 0x0f) << 2) | ((b3 & 0xc0) >>> 6)]);
      sb.append(BASE64_ENCODE_CHARS[b3 & 0x3f]);
    }
    return sb.toString();
  }
}